﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace itChatAdmin
{
    public partial class FormStranger : Form
    {
        String UserID = "";
        public FormStranger()
        {
            InitializeComponent();
        }

        private void button19_Click(object sender, EventArgs e)
        {
            Object command = new
            {
                cmd = WX_COMMAND.get_chatrooms.ToString(),
                data_0 = "",
                data_1 = "",
                data_2 = "",
                data_3 = ""
            };
            //向服务器发送数据
            String result = Form1.SendCommnadTo_itChat(command);
            DoWithGroupText(result);
 
        }

        private void DoWithGroupText(String wxMsg)
        {
            //检查过滤表
            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            if (Form1.GroupFilterdList.Count == 0)
            {
                mySQLiteCommand.CommandText = "SELECT *  FROM MelaGroups";
                SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
                while (mySQLiteDataReader.Read())
                {
                    Boolean IsGetStrangers = Boolean.Parse(mySQLiteDataReader["IsGetStrangers"].ToString().Trim());
                    if (IsGetStrangers == false)
                    {
                        Form1.GroupFilterdList.Add(mySQLiteDataReader["NickName"].ToString().Trim());
                    }
                }
                mySQLiteDataReader.Close();
            }

            //检查联系人表
            if (Form1.FriendList.Count == 0)
            {
                mySQLiteCommand.CommandText = "SELECT *  FROM MelaFriends";
                SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
                while (mySQLiteDataReader.Read())
                {
                    Form1.FriendList.Add(mySQLiteDataReader["NickName"].ToString().Trim());
                }
                mySQLiteDataReader.Close();
            }

            //检查陌生人列表
            if (Form1.StrangerList.Count == 0)
            {
                mySQLiteCommand.CommandText = "SELECT *  FROM MelaStranger";
                SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
                while (mySQLiteDataReader.Read())
                {
                    Form1.StrangerList.Add(mySQLiteDataReader["NickName"].ToString().Trim());
                }
                mySQLiteDataReader.Close();
            }


            //处理微信数据
            JArray array = JArray.Parse(wxMsg);
            foreach (JObject m in array)
            {
                String GroupNickName = m["NickName"].ToString().Trim();
                //String GroupUserName = m["UserName"].ToString().Trim();

                //检查该群是否要被过滤
                Boolean Filter = false;
                foreach (String filter in Form1.GroupFilterdList)
                {
                    if (filter == GroupNickName)
                    {
                        Filter = true;
                        break;
                    }
                }
                if (Filter == true) continue;


                String MemberLists = m["MemberList"].ToString().Trim();
                foreach (JObject MemberList in JArray.Parse(MemberLists))
                {
                    String UserName = MemberList["UserName"].ToString().Trim();
                    String RemarkPYQuanPin = MemberList["RemarkPYQuanPin"].ToString().Trim();
                    String DisplayName = MemberList["DisplayName"].ToString().Trim();
                    String KeyWord = MemberList["KeyWord"].ToString().Trim();
                    String PYInitial = MemberList["PYInitial"].ToString().Trim();
                    String Uin = MemberList["Uin"].ToString().Trim();
                    String MemberStatus = MemberList["MemberStatus"].ToString().Trim();
                    String PYQuanPin = MemberList["PYQuanPin"].ToString().Trim();
                    String RemarkPYInitial = MemberList["RemarkPYInitial"].ToString().Trim();
                    String NickName = MemberList["NickName"].ToString().Trim();
                    String AttrStatus = MemberList["AttrStatus"].ToString().Trim();

                    //检查联系人列表
                    Boolean UserIsExist = false;
                    foreach (String ss in Form1.FriendList)
                    {
                        if (NickName == ss)
                        {
                            UserIsExist = true;
                            break;
                        }
                    }
                    if (UserIsExist == true) continue;

                    ////检查陌生人列表
                    foreach (String ss in Form1.StrangerList)
                    {
                        if (NickName == ss)
                        {
                            UserIsExist = true;
                            break;
                        }
                    }

                    //写入系统
                    if (UserIsExist == false)
                    {
                        Form1.StrangerList.Add(NickName);
                    }


                    mySQLiteCommand.CommandText = "SELECT count(*) FROM MelaStranger WHERE NickName=@NickName";
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@NickName"));
                    mySQLiteCommand.Parameters["@NickName"].Value = UserName;

                    object i = mySQLiteCommand.ExecuteScalar();
                    mySQLiteCommand.Parameters.Clear();

                    if (int.Parse(i.ToString()) > 0)
                    {
                        mySQLiteCommand.CommandText = @"UPDATE MelaStranger SET 
UserName=@UserName,
RemarkPYQuanPin=@RemarkPYQuanPin,
DisplayName=@DisplayName,
KeyWord=@KeyWord,
PYInitial=@PYInitial,
Uin=@Uin,
MemberStatus=@MemberStatus,
PYQuanPin=@PYQuanPin,
RemarkPYInitial=@RemarkPYInitial,
AttrStatus=@AttrStatus
WHERE NickName=@NickName";
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@UserName"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkPYQuanPin"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@DisplayName"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@KeyWord"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@PYInitial"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Uin"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MemberStatus"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@PYQuanPin"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkPYInitial"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@AttrStatus"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@NickName"));


                        mySQLiteCommand.Parameters["@UserName"].Value = UserName;
                        mySQLiteCommand.Parameters["@RemarkPYQuanPin"].Value = RemarkPYQuanPin;
                        mySQLiteCommand.Parameters["@DisplayName"].Value = DisplayName;
                        mySQLiteCommand.Parameters["@KeyWord"].Value = KeyWord;
                        mySQLiteCommand.Parameters["@PYInitial"].Value = PYInitial;
                        mySQLiteCommand.Parameters["@Uin"].Value = Uin;
                        mySQLiteCommand.Parameters["@MemberStatus"].Value = MemberStatus;
                        mySQLiteCommand.Parameters["@PYQuanPin"].Value = PYQuanPin;
                        mySQLiteCommand.Parameters["@RemarkPYInitial"].Value = RemarkPYInitial;
                        mySQLiteCommand.Parameters["@NickName"].Value = NickName;
                        mySQLiteCommand.Parameters["@AttrStatus"].Value = AttrStatus;

                    }
                    else
                    {
                        mySQLiteCommand.CommandText = @"INSERT INTO MelaStranger (
UserName,
RemarkPYQuanPin,
DisplayName,
KeyWord,
PYInitial,
Uin,
MemberStatus,
PYQuanPin,
RemarkPYInitial,
NickName,
AttrStatus
) VALUES(
@UserName,
@RemarkPYQuanPin,
@DisplayName,
@KeyWord,
@PYInitial,
@Uin,
@MemberStatus,
@PYQuanPin,
@RemarkPYInitial,
@NickName,
@AttrStatus
) ";
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@UserName"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkPYQuanPin"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@DisplayName"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@KeyWord"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@PYInitial"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Uin"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MemberStatus"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@PYQuanPin"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@RemarkPYInitial"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@AttrStatus"));
                        mySQLiteCommand.Parameters.Add(new SQLiteParameter("@NickName"));


                        mySQLiteCommand.Parameters["@UserName"].Value = UserName;
                        mySQLiteCommand.Parameters["@RemarkPYQuanPin"].Value = RemarkPYQuanPin;
                        mySQLiteCommand.Parameters["@DisplayName"].Value = DisplayName;
                        mySQLiteCommand.Parameters["@KeyWord"].Value = KeyWord;
                        mySQLiteCommand.Parameters["@PYInitial"].Value = PYInitial;
                        mySQLiteCommand.Parameters["@Uin"].Value = Uin;
                        mySQLiteCommand.Parameters["@MemberStatus"].Value = MemberStatus;
                        mySQLiteCommand.Parameters["@PYQuanPin"].Value = PYQuanPin;
                        mySQLiteCommand.Parameters["@RemarkPYInitial"].Value = RemarkPYInitial;
                        mySQLiteCommand.Parameters["@NickName"].Value = NickName;
                        mySQLiteCommand.Parameters["@AttrStatus"].Value = AttrStatus;
                    }
                    mySQLiteCommand.ExecuteNonQuery();
                    Form1.WriteToDBInDisk(mySQLiteCommand);
                    mySQLiteCommand.Parameters.Clear();

                    //写入系统
                    Form1.StrangerList.Add(UserName);

                }
            }
            Refresh_ListView();
        }

        private void Refresh_ListView()
        {
            this.listView3.Items.Clear();
            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);

            if (this.textBox8.Text.Trim()=="")
            {
                mySQLiteCommand.CommandText = "SELECT *  FROM MelaStranger";
            }
            else
            {
                mySQLiteCommand.CommandText = "SELECT *  FROM MelaStranger WHERE NickName like '%"+ this.textBox8.Text.Trim() + "%'";
            }
            SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
            mySQLiteCommand.Parameters.Clear();
            while (mySQLiteDataReader.Read())
            {
                String ID = mySQLiteDataReader["ID"].ToString().Trim();
                String NickName = mySQLiteDataReader["NickName"].ToString().Trim();
                String IsSend = mySQLiteDataReader["IsSend"].ToString().Trim();
                int GroupsIndex = 0;
                if (Boolean.Parse(IsSend) == true)
                {
                    GroupsIndex = 0;
                }
                else
                {
                    GroupsIndex = 1;
                }

                ListViewItem item = new ListViewItem(new string[] { ID, NickName }, 0, this.listView3.Groups[GroupsIndex]);
                this.Invoke((EventHandler)delegate
                {
                    listView3.Items.Add(item);
                });
            }
            mySQLiteDataReader.Close();
        }

        private void FormStranger_Load(object sender, EventArgs e)
        {
            Refresh_ListView();
        }

        private void 启用发送ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (listView3.SelectedIndices != null && listView3.SelectedIndices.Count > 0)
            {
                ListView.SelectedIndexCollection c = listView3.SelectedIndices;
                UserID = listView3.Items[c[0]].SubItems[0].Text;
            }

            String UserName1=Form1.GetStrangerUserNameByID(UserID);
            Object command = new
            {
                cmd = WX_COMMAND.add_friend.ToString(),
                data_0 = UserName1,
                data_1 = 2,
                data_2 = "你好，你的签名很有正能量，互粉一下吧！",
                data_3 = ""

            };
            //向服务器发送数据
            String result = Form1.SendCommnadTo_itChat(command);
            MessageBox.Show(result);
            Form1.Write_Log(result, "add_friend", 4);
            JObject json = JObject.Parse(result);

            if (json["BaseResponse"]["Ret"].ToString().Trim() != "0")
            {
                SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
                mySQLiteCommand.CommandText = "UPDATE MelaStranger SET IsSend=1";
                mySQLiteCommand.ExecuteNonQuery();
                Form1.WriteToDBInDisk(mySQLiteCommand);
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            Refresh_ListView();
        }
    }
}
